package com.bogewang.member.service.impl;

import com.bogewang.core.base.BaseApiService;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.constants.Constants;
import com.bogewang.core.core.token.TokenUtils;
import com.bogewang.core.core.type.TypeCastHelper;
import com.bogewang.core.core.utils.MD5Util;
import com.bogewang.member.entity.LoginDTO;
import com.bogewang.member.entity.UserDTO;
import com.bogewang.member.mapper.UserMapper;
import com.bogewang.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberServiceImpl extends BaseApiService<UserDTO> implements MemberService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenUtils tokenUtils;

    @Override
    public BaseResponse<UserDTO> existMobile(@RequestParam("mobile") String mobile) throws Exception {
        //1.验证参数;
        if (isBlank(mobile)) {
            return setResultError("手机不能为空!");
        }

        UserDTO userDTO = userMapper.existMobile(mobile);
        //2.查询;
        if (userDTO == null) {
            return setResultError(Constants.HTTP_RES_CODE_203, "用户信息不存在!");
        }
        //3.返回;
        return setResultSuccess(userDTO);
    }

    @Override
    public BaseResponse<UserDTO> getInfo(String token) {
        // 1.验证token参数
        if (isBlank(token)){
            return setResultError("token不能为空!");
        }

        // 2.使用token查询redis 中的userId
        String redisUserid = tokenUtils.getToken(token);
        if (isBlank(redisUserid)){
            return setResultError("token错误!");
        }
        // 3.使用userID查询 数据库用户信息
        long userid = TypeCastHelper.toLong(redisUserid);
        UserDTO userDTO = userMapper.findByUserId(userid);
        if (userDTO == null){
            return setResultError("用户不存在!");
        }

        return setResultSuccess(userDTO);
    }

    @Override
    public BaseResponse<UserDTO> ssoLogin(@RequestBody LoginDTO loginDTO) {
        // 1.验证参数
        String mobile = loginDTO.getMobile();
        if (isBlank(mobile)) {
            return setResultError("手机号码不能为空!");
        }
        String password = loginDTO.getPassword();
        if (isBlank(password)) {
            return setResultError("密码不能为空!");
        }
        // 判断登陆类型
        String loginType = loginDTO.getLoginType();
        if (isBlank(loginType)) {
            return setResultError("登陆类型不能为空!");
        }
        // 目的是限制范围
        if (!(loginType.equals(Constants.MEMBER_LOGIN_TYPE_ANDROID) || loginType.equals(Constants.MEMBER_LOGIN_TYPE_IOS)
                || loginType.equals(Constants.MEMBER_LOGIN_TYPE_PC))) {
            return setResultError("登陆类型出现错误!");
        }

        // 设备信息
        String deviceInfo = loginDTO.getDeviceInfo();
        if (isBlank(deviceInfo)) {
            return setResultError("设备信息不能为空!");
        }
        // 2.对登陆密码实现加密
        String newPassWord = MD5Util.MD5(password);
        // 3.使用手机号码+密码查询数据库 ，判断用户是否存在
        UserDTO login = userMapper.login(mobile, newPassWord);
        if (login == null) {
            return setResultError("用户名称或者密码错误!");
        }
        return setResultSuccess(login);
    }
}
